<?php

class Twitter {
	
	public $username;
	public $password;
	public $host = "http://twitter.com/";
	public $curl;
	public $result;
	public $host_methods = array(
	                            'status'=> 'statuses/user_timeline',
								'friends_status' => 'statuses/friends_timeline',
								'set_status' => 'statuses/update',
								'send_dm' => 'direct_messages/new'
								);
	public $format = 'json';
	public $cache_time = 60; //1 min
	
	public function __construct($format='json', $username='foo', $password='bar') {
		$this->username = $username;
		$this->password = $password;
		$this->format = $format;
		//setup curl session
		$this->curl = curl_init();
		curl_setopt($this->curl, CURLOPT_VERBOSE, 1);
		curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($this->curl, CURLOPT_USERPWD, $this->username.':'.$this->password);
		curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
	}
	
	public function __destruct(){curl_close($this->curl);}
	
	public function __call($func, $params){
		if(method_exists($this, $func)) return $this->$func($params);
		elseif(in_array($func, array_flip($this->host_methods)) && is_array($params)) return $this->call($func, $params);
		else return false;
	}
	
	public function call($method, $args){
		if(!in_array($method, array_flip($this->host_methods)) || !is_array($args)) return false;
		$url = $this->host . $this->host_methods[$method].'.'.$this->format.'?';

		if($cache  = $this->get_cache($method)) return $cache;
		foreach($args as $name=>$val){
			if(is_array($val)){
				foreach($val as $field=>$value) $url .= $field.'='.urlencode(stripslashes(urldecode($value))).'&';
			}else $url .= $name.'='.urlencode(stripslashes(urldecode($val))).'&';			
		}
		
		curl_setopt($this->curl, CURLOPT_URL, $url);
		$exec = curl_exec($this->curl);
		$info = curl_getInfo($this->curl);
		if($info['http_code'] == 200){
			$this->set_cache($method, $exec);			
			return $exec;
		}else return false;
		
	}
	
	public function valid_cache($method){
		if($this->excluded_from_cache($method)) return false;
		$cache_file = $this->cache_file($method);
		if(is_readable($cache_file)) $mod_time = filemtime($cache_file);
		else $mod_time = 0;
		$diff = time() - $mod_time;
		if($diff < $this->cache_time) return $cache_file;
		else return false;
	}
	
	public function get_cache($method){
		if($cache_file = $this->valid_cache($method)) return file_get_contents($cache_file);
		else return false;
	}
	
	public function set_cache($method, $content){
		$this->result = $content;
		if($this->excluded_from_cache($method)) return false;
		if(!is_readable(CACHE_DIR .'twitter/')){
			mkdir(CACHE_DIR .'twitter/');
			chmod(CACHE_DIR .'twitter/', 0777);
		}
		return file_put_contents($this->cache_file($method), $content);
	}
	
	public function cache_file($method){
		return CACHE_DIR .'twitter/'.$method.'.'.$this->format;
	}
	
	public function excluded_from_cache($method){
		if(substr_count($method, 'set_') || substr_count($method, 'send_') || 
				substr_count($method, 'update') || substr_count($method, 'new')) return true;
		else return false;
	}
}
?>
